﻿using CaptchaGen;
using HPIT.RentHouse.Common;
using HPIT.RentHouse.DTO;
using HPIT.RentHouse.IService;
using HPIT.RentHouse.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace HPIT.RentHouse.Admin.Controllers
{
    public class HomeController : Controller
    {
        private IAdminUsersService _adminUsersService;
        public HomeController(IAdminUsersService adminUsersService)
        {
            _adminUsersService = adminUsersService;
        }
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Login()
        {
            return View();
        }
        public ActionResult GetVerCode()
        {
            //安装-CaptchaGen插件
            //封装生成随机码的方法(CommonHelper中已经封装好)
            string cade = CommonHelper.CreateVerifyCode(4);
            //临时保存code, 保存数据时需要对比校验
            TempData["cade"] = cade;
            //调用ImageFactory.Generatelmage(验证码文字宽度高度，字体大小，扭曲程度(越大越扭曲))
            var src = ImageFactory.GenerateImage(cade, 50, 100, 15, 6);
            //返回文件File(src, type)
            return File(src, "image/jpeg");
        }
        [HttpPost]
        public ActionResult SubmitLogin(LoginDTO dto)
        {
            if (TempData["cade"] == null)
            {
                return Json(new AjaxResult(ResultState.Error, "验证码已失效"));
            }
            else
            {
                string code = TempData["cade"].ToString();
                if (code != dto.VerCode)
                {
                    return Json(new AjaxResult(ResultState.Error, "验证码输入有误"));
                }
                else
                {
                    var result = _adminUsersService.Login(dto);
                    return Json(result);
                }
            }
        }
        /// <summary>
        /// 退出登录
        /// </summary>
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Login");
        }
    }
    }